﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace HY.DAL.oa
{
    /// <summary>
    /// 数据访问类:goods
    /// </summary>
    public partial class goods
    {
        public goods()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int sid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from review_goods");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HY.Model.oa.goods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into review_goods(");
            strSql.Append("gbh,item,unit,qty,price,total,pbh,pname,edt,adt,rname,rbh,remark)");
            strSql.Append(" values (");
            strSql.Append("@gbh,@item,@unit,@qty,@price,@total,@pbh,@pname,@edt,@adt,@rname,@rbh,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@gbh", SqlDbType.NVarChar,100),
					new SqlParameter("@item", SqlDbType.NVarChar,200),
					new SqlParameter("@unit", SqlDbType.NVarChar,100),
					new SqlParameter("@qty", SqlDbType.NVarChar,100),
					new SqlParameter("@price", SqlDbType.Float,8),
					new SqlParameter("@total", SqlDbType.Float,8),
					new SqlParameter("@pbh", SqlDbType.NVarChar,100),
					new SqlParameter("@pname", SqlDbType.NVarChar,100),
					new SqlParameter("@edt", SqlDbType.Date,3),
					new SqlParameter("@adt", SqlDbType.Date,3),
					new SqlParameter("@rname", SqlDbType.NVarChar,100),
					new SqlParameter("@rbh", SqlDbType.NVarChar,100),
					new SqlParameter("@remark", SqlDbType.NVarChar,1000)};
            parameters[0].Value = model.gbh;
            parameters[1].Value = model.item;
            parameters[2].Value = model.unit;
            parameters[3].Value = model.qty;
            parameters[4].Value = model.price;
            parameters[5].Value = model.total;
            parameters[6].Value = model.pbh;
            parameters[7].Value = model.pname;
            parameters[8].Value = model.edt;
            parameters[9].Value = model.adt;
            parameters[10].Value = model.rname;
            parameters[11].Value = model.rbh;
            parameters[12].Value = model.remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(HY.Model.oa.goods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update review_goods set ");
            strSql.Append("gbh=@gbh,");
            strSql.Append("item=@item,");
            strSql.Append("unit=@unit,");
            strSql.Append("qty=@qty,");
            strSql.Append("price=@price,");
            strSql.Append("total=@total,");
            strSql.Append("pbh=@pbh,");
            strSql.Append("pname=@pname,");
            strSql.Append("edt=@edt,");
            strSql.Append("adt=@adt,");
            strSql.Append("rname=@rname,");
            strSql.Append("rbh=@rbh,");
            strSql.Append("remark=@remark");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@gbh", SqlDbType.NVarChar,100),
					new SqlParameter("@item", SqlDbType.NVarChar,200),
					new SqlParameter("@unit", SqlDbType.NVarChar,100),
					new SqlParameter("@qty", SqlDbType.NVarChar,100),
					new SqlParameter("@price", SqlDbType.Float,8),
					new SqlParameter("@total", SqlDbType.Float,8),
					new SqlParameter("@pbh", SqlDbType.NVarChar,100),
					new SqlParameter("@pname", SqlDbType.NVarChar,100),
					new SqlParameter("@edt", SqlDbType.Date,3),
					new SqlParameter("@adt", SqlDbType.Date,3),
					new SqlParameter("@rname", SqlDbType.NVarChar,100),
					new SqlParameter("@rbh", SqlDbType.NVarChar,100),
					new SqlParameter("@remark", SqlDbType.NVarChar,1000),
					new SqlParameter("@sid", SqlDbType.Int,4)};
            parameters[0].Value = model.gbh;
            parameters[1].Value = model.item;
            parameters[2].Value = model.unit;
            parameters[3].Value = model.qty;
            parameters[4].Value = model.price;
            parameters[5].Value = model.total;
            parameters[6].Value = model.pbh;
            parameters[7].Value = model.pname;
            parameters[8].Value = model.edt;
            parameters[9].Value = model.adt;
            parameters[10].Value = model.rname;
            parameters[11].Value = model.rbh;
            parameters[12].Value = model.remark;
            parameters[13].Value = model.sid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int sid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from review_goods ");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from review_goods ");
            strSql.Append(" where sid in (" + sidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HY.Model.oa.goods GetModel(int sid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sid,gbh,item,unit,qty,price,total,pbh,pname,edt,adt,rname,rbh,remark from review_goods ");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            HY.Model.oa.goods model = new HY.Model.oa.goods();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sid"] != null && ds.Tables[0].Rows[0]["sid"].ToString() != "")
                {
                    model.sid = int.Parse(ds.Tables[0].Rows[0]["sid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["gbh"] != null && ds.Tables[0].Rows[0]["gbh"].ToString() != "")
                {
                    model.gbh = ds.Tables[0].Rows[0]["gbh"].ToString();
                }
                if (ds.Tables[0].Rows[0]["item"] != null && ds.Tables[0].Rows[0]["item"].ToString() != "")
                {
                    model.item = ds.Tables[0].Rows[0]["item"].ToString();
                }
                if (ds.Tables[0].Rows[0]["unit"] != null && ds.Tables[0].Rows[0]["unit"].ToString() != "")
                {
                    model.unit = ds.Tables[0].Rows[0]["unit"].ToString();
                }
                if (ds.Tables[0].Rows[0]["qty"] != null && ds.Tables[0].Rows[0]["qty"].ToString() != "")
                {
                    model.qty = ds.Tables[0].Rows[0]["qty"].ToString();
                }
                if (ds.Tables[0].Rows[0]["price"] != null && ds.Tables[0].Rows[0]["price"].ToString() != "")
                {
                    model.price = decimal.Parse(ds.Tables[0].Rows[0]["price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["total"] != null && ds.Tables[0].Rows[0]["total"].ToString() != "")
                {
                    model.total = decimal.Parse(ds.Tables[0].Rows[0]["total"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pbh"] != null && ds.Tables[0].Rows[0]["pbh"].ToString() != "")
                {
                    model.pbh = ds.Tables[0].Rows[0]["pbh"].ToString();
                }
                if (ds.Tables[0].Rows[0]["pname"] != null && ds.Tables[0].Rows[0]["pname"].ToString() != "")
                {
                    model.pname = ds.Tables[0].Rows[0]["pname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["edt"] != null && ds.Tables[0].Rows[0]["edt"].ToString() != "")
                {
                    model.edt = DateTime.Parse(ds.Tables[0].Rows[0]["edt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adt"] != null && ds.Tables[0].Rows[0]["adt"].ToString() != "")
                {
                    model.adt = DateTime.Parse(ds.Tables[0].Rows[0]["adt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["rname"] != null && ds.Tables[0].Rows[0]["rname"].ToString() != "")
                {
                    model.rname = ds.Tables[0].Rows[0]["rname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["rbh"] != null && ds.Tables[0].Rows[0]["rbh"].ToString() != "")
                {
                    model.rbh = ds.Tables[0].Rows[0]["rbh"].ToString();
                }
                if (ds.Tables[0].Rows[0]["remark"] != null && ds.Tables[0].Rows[0]["remark"].ToString() != "")
                {
                    model.remark = ds.Tables[0].Rows[0]["remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sid,gbh,item,unit,qty,price,total,pbh,pname,edt,adt,rname,rbh,remark ");
            strSql.Append(" FROM review_goods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sid,gbh,item,unit,qty,price,total,pbh,pname,edt,adt,rname,rbh,remark ");
            strSql.Append(" FROM review_goods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM review_goods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.sid desc");
            }
            strSql.Append(")AS Row, T.*  from review_goods T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "review_goods";
            parameters[1].Value = "sid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

